package binaryOctopus;
import java.util.Iterator;
import java.util.Random;
import java.util.ArrayList;

/**
 * The AI class is has a casual level of intelligence to play a
 * game of checkers. It is a type of player in the case that it
 * must be able to move the pieces on the board. 
 */

public class AI implements Player {
	//pieces that the AI has
	private ArrayList<Coordinates> pieces;
	//passed the pieces on board to AI because it needs to
	//know where pieces on board are for intelligence purposes
	int[][] piecesOnBoard;

	
	public AI(){
		
	}
	
	
	public AI(ArrayList<Coordinates> pieces, int[][] board){
    	this.pieces = pieces;
    	piecesOnBoard = board;
	}

    
    
    public void takeTurn(){
    	Iterator<Coordinates> iter = pieces.iterator();
    	while(iter.hasNext()){
    		Coordinates next = iter.next();
    		jump(next);
    	}
    	move();
    }
    
    
    private void jump(Coordinates next){
    		
    	boolean[] jumpTable = canJump(next);
    	int isKing = piecesOnBoard[next.getColumn()][next.getRow()];
    	boolean jumpLeft = jumpTable[0], jumpRight = jumpTable[1];
    	boolean jumpDown = jumpTable[2], jumpUp = jumpTable[3];
    		
    		
    	if(jumpDown && jumpLeft && (piecesOnBoard[next.getColumn() - 2][next.getRow() - 2] == 0)){
    		if(piecesOnBoard[next.getColumn() - 1][next.getRow() - 1] != 0){
   				//check if piece is enemy
    			//delete piece it jumps, move piece's coordinates
    			//new coords: (C: -2, R: -2)
    			//switch turns
    			return;
    		}
    	}
    	if(jumpDown && jumpRight && (piecesOnBoard[next.getColumn() + 2][next.getRow() - 2] == 0)){
    		if(piecesOnBoard[next.getColumn() + 1][next.getRow() - 1] != 0){
   				//check if piece is enemy
    			//delete piece it jumps, move piece's coordinates
    			//new coords: (C: +2, R: -2)
    			//switch turns
    			return;
    		}
    	}
   		if((isKing == 2) && jumpUp && jumpLeft && (piecesOnBoard[next.getColumn() - 2][next.getRow() + 2] == 0)){
   			if(piecesOnBoard[next.getColumn() - 1][next.getRow() + 1] != 0){
   				//check if piece is enemy
    			//delete piece it jumps, move piece's coordinates
    			//new coords: (C: -2, R: +2)
    			//switch turns
   				return;
   			}
    	}
    	if((isKing == 2) && jumpUp && jumpRight && (piecesOnBoard[next.getColumn() + 2][next.getRow() + 2] == 0)){
    		if(piecesOnBoard[next.getColumn() + 1][next.getRow() + 1] != 0){
   				//check if piece is enemy
    			//delete piece it jumps, move piece's coordinates
    			//new coords: (C: +2, R: +2)
    			//switch turns
    			return;
    		}
    	}	
    }
    
    
    
    /**
    * Moves pieces on the board
    * picks a random piece and moves it in a random valid direction
    */ 
    public void/*Coordinates[]*/ move(){
        //Needs to return an array of coordinates
        //index 0 must contain the coordinates of the piece that is going to be moved
        //index 1 must be the new location of the piece
        int movingPieces = 0;
        Iterator<Coordinates> iter = pieces.iterator();
    	while(iter.hasNext()){
    		Coordinates current = iter.next();
    		boolean move = false;
    		
    		boolean[] moveTable = canMove(current);
        	int isKing = piecesOnBoard[current.getColumn()][current.getRow()];
    		boolean moveLeft = moveTable[0], moveRight = moveTable[1];
    		boolean moveDown = moveTable[2], moveUp = moveTable[3];
    		
    		if(moveDown && moveLeft){
    			if(piecesOnBoard[current.getColumn() - 1][current.getRow() - 1] == 0){
    				//move piece's coordinates
        			//new coords: (C: -1, R: -1)
        			//switch turns
    				move = true;
    			}
    		}
    		if(moveDown && moveRight){
    			if(piecesOnBoard[current.getColumn() + 1][current.getRow() - 1] == 0){
    				//move piece's coordinates
        			//new coords: (C: +1, R: -1)
        			//switch turns
    				move = true;
    			}
    		}
    		if((isKing == 2) && moveUp && moveLeft){
    			if(piecesOnBoard[current.getColumn() - 1][current.getRow() + 1] == 0){
    				//move piece's coordinates
        			//new coords: (C: -1, R: +1)
        			//switch turns
    				move = true;
    			}
    		}
    		if((isKing == 2) && moveUp && moveRight){
    			if(piecesOnBoard[current.getColumn() + 1][current.getRow() + 1] == 0){
    				//move piece's coordinates
        			//new coords: (C: +1, R: +1)
        			//switch turns
    				move = true;
    			}
    		}
    		/*if(move == true){
    			movingPieces++;
    		}*/
    	}
        //Random randGen = new Random();
        //int chosenOne = randGen.nextInt(movingPieces);
        
        //Coordinates[] 
    
    	
        //return null;
    }

    
    
    /**
    * Checks if there are any valid jumps for any of the pieces
    * if there is, do it and switch turn.
    */
    private boolean[] canJump(Coordinates chosenOne){
    	boolean[] matrix = new boolean[4];
        for(int i = 0; i < 4; i++){
            if(chosenOne.getColumn() - 2 != -1){
            	matrix[0] = true;}
            if(chosenOne.getColumn() + 2 != 8){
            	matrix[1] = true;}
            if(chosenOne.getRow() - 2 != -1){
            	matrix[2] = true;}
            if(chosenOne.getRow() + 2 != 8){
            	matrix[3] = true;}
        }
        return matrix;
    }
    
    
    private boolean[] canMove(Coordinates chosenOne){
    	boolean[] matrix = new boolean[4];
        for(int i = 0; i < 4; i++){
            if(chosenOne.getColumn() - 1 != -1){
            	matrix[0] = true;}
            if(chosenOne.getColumn() + 1 != 8){
            	matrix[1] = true;}
            if(chosenOne.getRow() - 1 != -1){
            	matrix[2] = true;}
            if(chosenOne.getRow() + 1 != 8){
            	matrix[3] = true;}
        }
        return matrix;
    }
 

    public static void main(String[] args){
        
    }
}

